<?php



/**

 * @Author: kendy

 * @Date:   2019-11-26 10:36:56

 * @Last Modified by:   jiayanting

 * @Last Modified time: 2020-04-21 10:31:28

 */

namespace app\common\model; 

use think\Db;

use think\Model;

use app\api\model\Method as Met;

class FriendsCircleModel extends Model

{

	/**

	 * [getFriendsCircleList 获取友圈列表]

	 * @Author   kendy

	 * @DateTime 2019-11-27T08:35:31+0800

	 * @param    [type]                   $tag_id [标签id]

	 * @param    [type]                   $page   [当前页数]

	 * @param    string                   $limit  [每页显示条数]

	 * @param    string                   $field  [要获取的字段]

	 * @return   [type]                           [description]

	 */

	public function getFriendsCircleList($tag_id,$u_id,$page,$limit='',$field='',$onlyList='',$f_id=''){ 

        $orderBy = input('order_by','1','trim');     

        $order = 1;

        $limit = ($limit=='')?config('page_size'):$limit;

        //获取友圈标签列表

        $tagList = Db::name('fc_tags')

        ->field('id,data_name')

        ->where(['is_display'=>0])

        ->order('data_sort desc')

        ->select();   

        $result = []; 

        $where = [];

        $method = new Met();

        if(empty($onlyList))

        {

             //固定的旅游攻略

            $lv[0]['id'] = 0;

            $lv[0]['data_name'] = '旅游攻略';  

            foreach ($lv as $key => $value) {    

                 array_unshift($tagList, $value);

            }

            $result['tagList'] = $tagList; 

        }  

        $tag_id= ($tag_id=='')?0:$tag_id; 
        $where = [];
        if(!empty($f_id)){
            $where['f_id'] = ['neq',$f_id];
        } 
        if(!empty($tag_id))

        {

        	//用户头像，用户昵称，距离发布时间

        	$field= ($field==='')?"f_id as id,fc.uid,fc.fc_title as title,fc.image,fc.video_covers,fc.video,content,user_icon,nickname,fc.create_time,set_like_num+like_num like_num,set_share_num + share_num share_num,fc.status":$field;

	        $orderFile = array('1'=>'fc.create_time','2'=>'like_num');

	        $orderSort = array('0'=>'asc','1'=>'desc');

	        $order = array($orderFile[$orderBy]=>$orderSort[$order]); 

	           $fcList = Db::name('friends_circle') 

	           ->alias('fc')

	           ->join('user u','u.uid=fc.uid','left')

	           ->where(['fc_id'=>$tag_id,'is_pass'=>1,'fc.is_del'=>0,'is_display'=>0]) 
               ->where($where)

	           ->field($field)

	           ->order($order)

               ->order('fc.create_time desc') 

	           ->limit(($page - 1) * $limit, $limit)

	           ->select();     

	           if(count($fcList)>0)

	           { 

	           	 foreach ($fcList as $k => $v) {

                    $fcList[$k]['image'] = [];

	           	 	 //刚刚，小时，天，

                     $fcList[$k]['create_time'] = format_date(strtotime($v['create_time'])); 

                     if(!empty($v['image']))

	           	 	 $fcList[$k]['image'] = array_slice(explode(',', $v['image']),0,3); 

                     $fcList[$k]['type'] = 2;    

                     //查看该友圈的评论数 

                     $fcList[$k]['comment_num'] = Db::name('comment_circle')

                     ->where(['is_display'=>0,'is_del'=>0,'wid'=>$v['id'],'pid'=>0])

                     ->count();    

                     $fcList[$k]['like_status'] = 0;

	           	 	 //查看该用户是否登陆，登陆是否点赞

	           	 	 if($u_id>0)

	           	 	 {

                        $fcList[$k]['like_status'] = Db::name('log_like')->where(['uid'=>$u_id,'object_id'=>$v['id'],'type'=>2])->count();

	           	 	 }   

	           	 }

	           }

	          $result['fcList'] = $fcList;  

	          return $result; 

	      }

          else

          {  

            //旅游攻略

            $fcList= $method->method_list(1,$page,$limit,$orderBy);

            if(count($fcList)>0)

            {

                foreach ($fcList as $key => $value) {

                     $fcList[$key]['like_status'] = 0;  

                    if($u_id>0)

                     {

                        $fcList[$key]['like_status'] = Db::name('log_like')

                        ->where(['uid'=>$u_id,'object_id'=>$value['id'],'type'=>1])

                        ->count();

                     }

                  }

            }

            $result['fcList'] = $fcList;

            return $result; 

          }

	       return $result;

	}





        /**

         * [addLikeNum 新增点赞]

         * @Author   kendy

         * @DateTime 2019-11-27T08:36:32+0800

         * @param    [type]                   $fc_id [description]

         * @param    [type]                   $u_id  [description]

         */

        public function addLikeNum($object_id,$u_id,$type){  

             if($type==1) //攻略

             { 

                 $isFind = false;

                 $c = Db::name('method')

                 ->where(['is_display'=>0,'is_del'=>0,'is_pass'=>1,'mid'=>$object_id])

                 ->count();

                 $isFind = ($c>0);

                 if(!$isFind)  return apiReturn('400','无效的点赞对象',0);

                 $data = [];

                 $data['uid'] = $u_id;

                 $data['object_id'] = $object_id;

                 $data['type'] = 1;
                 $data['send_user'] = Db::name('method')->where('mid',$object_id)->value("uid");  

                 //判断是否已经关注

                 $isLike = Db::name('log_like')

                 ->where($data)

                 ->count();

                 if($isLike>0) return apiReturn('200','点赞成功',0);

                 $data['create_time'] = date('Y-m-d H:i:s'); 
                 Db::startTrans();

                 try{

                     $rs = Db::name('log_like')

                     ->insert($data);

                     if(false !== $rs)

                     {

                          //更新友圈点赞数

                         Db::name('method')

                         ->where('mid',$object_id)

                         ->setInc('like_num',1);

                         Db::commit();

                        return apiReturn('200','点赞成功',0);

                     }

                   

                 }catch(\Exception $e){

                    Db::rollback();

                 }  

             }

             else if($type==2) //2是友圈

             {

                 $isFind = false;

                 $c = Db::name('friends_circle')

                 ->where(['is_pass'=>1,'is_del'=>0,'f_id'=>$object_id])

                 ->count();

                 $isFind = ($c>0);

                 if(!$isFind)  return apiReturn('400','无效的点赞对象',0);

                 $data = [];

                 $data['uid'] = $u_id;

                 $data['object_id'] = $object_id;

                 $data['type'] = 2; 

                 $data['send_user'] = Db::name('friends_circle')->where('f_id',$object_id)->value("uid");  
                 //判断是否已经关注

                 $isLike = Db::name('log_like')

                 ->where($data)

                 ->count();

                 if($isLike>0) return apiReturn('200','点赞成功',0);

                 $data['create_time'] = date('Y-m-d H:i:s'); 
                 Db::startTrans();

                 try{

                     $rs = Db::name('log_like')

                     ->insert($data);

                     if(false !== $rs){

                        //更新友圈点赞数

                         Db::name('friends_circle')

                         ->where('f_id',$object_id)

                         ->setInc('like_num',1);

                         Db::commit();

                         return apiReturn('200','点赞成功',0);

                     }

                 }catch(\Exception $e) {

                    Db::rollback(); 

                 } 

             }

             return apiReturn('400','点赞失败',0);

        }

 

       

        /**

         * [cancelLikeNum 取消点赞]

         * @Author   kendy

         * @DateTime 2019-11-27T09:13:12+0800

         * @param    [type]                   $fc_id [友圈ID]

         * @param    [type]                   $u_id  [用户id]

         * @return   [type]                          [description]

         */

        public function cancelLikeNum($object_id,$u_id,$type){ 

            if($type==1) //攻略

             {  

                 Db::startTrans();

                 try{

                     $res = Db::name('log_like')

                     ->where(['uid'=>$u_id,'object_id'=>$object_id,'type'=>1])

                     ->delete(); 

                     if($res)

                     {

                          Db::name('method')

                         ->where('mid',$object_id)

                         ->setDec('like_num',1);

                        return apiReturn('200','取消点赞成功',0);

                     }

                   

                 }catch(\Exception $e){

                    Db::rollback();

                 }  

             }

             else if($type==2) //2是友圈

             {

                  

                 Db::startTrans();

                 try{

                      $res = Db::name('log_like')

                     ->where(['uid'=>$u_id,'object_id'=>$object_id,'type'=>2])

                     ->delete();  

                     if($res){

                        //更新友圈点赞数

                         Db::name('friends_circle')

                         ->where('f_id',$object_id)

                         ->setDec('like_num',1);

                         Db::commit();

                         return apiReturn('200','取消点赞成功',0);

                     }

                 }catch(\Exception $e) {

                    Db::rollback(); 

                 } 

             }

             return apiReturn('400','请勿重复取消点赞',0);

        } 

        

     



     public function getFriendsCircleInfo($f_id,$uid){

        //查看类型

        $result = Db::name('friends_circle')

        ->alias('a')

        ->field('a.f_id,a.fc_title,a.fc_id,a.uid,a.fc_content,a.video,video_content,a.video_covers,set_like_num+like_num as like_num,set_collect_num+collect_num as collect_num,a.create_time,u.user_icon,u.nickname,a.status')

        ->join('user u', 'a.uid=u.uid', 'left')

        ->where(['a.is_pass'=>1,'a.is_del'=>0,'a.is_display'=>0,'f_id'=>$f_id])

        ->find();

        $u_id = input('post.uid/d'); 

        $page = input('post.page/d'); 

        $limit = input('post.limit/d');

        if(empty($result))

            return apiReturn('400','无效的友圈信息',0);

        //查看当前友圈是否可以点赞，可以收藏，

         $result['like_status'] = 0;

         $result['collect_status'] = 0; 

         if(!empty($uid)){

             $result['video_covers'] = $result['video_covers'];

             $like_status = Db::name('log_like')

             ->where(['uid'=>$uid,'object_id'=>$f_id,'type'=>2])

             ->count();

             if($like_status>0) $result['like_status'] = 1;

             $collect_status = Db::name('collect')

             ->where(['uid'=>$uid,'other_id'=>$f_id,'type'=>6])

             ->count();

             if($collect_status>0) $result['collect_status'] = 1;

         }

        $result['comment_num'] = Db::name('comment_circle')

        ->where(['pid'=>0,'wid'=>$f_id,'is_display'=>0,'is_del'=>0])

        ->count();  

        //距离今日时间段 

        $result['create_time'] = format_date(strtotime($result['create_time']));

        //分类名称

        $result['data_name'] = Db::name('fc_tags')->where('id',$result['fc_id'])->value('data_name'); 

        //推荐友圈

        if($result['status']==2)

        {

             $recommend = $this->getFriendsCircleList($result['fc_id'],$u_id,1,3,'',1,$f_id);  

             $result = array_merge($result,$recommend);

        }

        unset($result['fc_id']);

        unset($result['uid']);

        return apiReturn('200','返回成功',$result); 

     }



    /**

     * [addFriendsCircle 发布友圈]

     * @Author   kendy

     * @DateTime 2019-12-10T09:27:15+0800

     * @param    [type]                   $data [数据]

     */

     public function addFriendsCircle($data){

           if(count($data)<0) return [];

           $result = Db::name('friends_circle')

           ->insert($data);

           if(false == $result)

            return apiReturn('400','发布失败',0);

            else 

            return apiReturn('200','发布成功',1);  

     }



}







